<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="赛事标题"
                    prop="title">
        <el-input v-model="dataForm.title"></el-input>
      </el-form-item>
      <el-form-item label="状态"
                    prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">公布</el-radio>
          <el-radio :label="0">撤销</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="轮播设置"
                    prop="isBanner">
        <el-radio-group v-model="dataForm.isBanner">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="比赛时间"
                    prop="gameTime">
        <el-date-picker
          v-model="dataForm.gameTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="封面图片"
                    prop="imgUrl">
        <pic-upload v-model="dataForm.imgUrl"></pic-upload>
      </el-form-item>
      <el-form-item label="赛事内容"
                    prop="content">
        <tiny-mce v-model="dataForm.content"></tiny-mce>
      </el-form-item>

    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button size="small"
                 @click="visible = false">取消</el-button>
      <el-button size="small"
                 type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import TinyMce from '@/components/tiny-mce'
import PicUpload from '@/components/pic-upload'
export default {
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: '',
        title: '',
        imgUrl: '',
        content: '',
        status: 1,
        isBanner: 0,
        gameTime: ''
      },
      dataRule: {
        title: [
          { required: true, message: '赛事标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    TinyMce,
    PicUpload
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.content = '<p></p>'
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/game/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            this.dataForm = data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/game'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
